#!/bin/bash
#################################################################
# @Description: 用例名称：支持虚拟机CPU和I/O线程绑定，测试目的：支持虚拟机CPU和I/O线程绑定
# @Date : 2023-11-24
# @Last Modified by :
# @Last Modified time: 2023-11-27
#################################################################
source "../common.sh"
export LANG=en_US.UTF-8
res=0
\cp ../xml/*.xml /opt/ 
# 指定虚拟机文件路径
vm3_name="vm3"
vm3_xml="/opt/${vm3_name}.xml"
cleanup_environment > /dev/null 2>&1
echo "虚拟机${vm3_name}配置文件关键配置信息如下："
cat /opt/${vm3_name}.xml| grep "cpuset"
cat /opt/${vm3_name}.xml| grep "iothread"
if [ $? -ne 0 ]; then
  echo "不能找到 cpuset的相关信息"
  exit 1
fi
echo "创建并启动虚拟机${vm3_name}"
virsh define $vm3_xml > /dev/null || res=1
virsh start $vm3_name > /dev/null || res=1
# 循环查询虚拟机的 IP 地址
vnc_port=$(virsh vncdisplay $vm3_name)
echo "等待获取$vm3_name的IP中..."
while true; do
    ip_vm3=$(get_vm_ip $vm3_name)
    # 如果有IP地址，打印信息并写入文件
    if [ -n "$ip_vm3" ]; then
        check_and_ping $ip_vm3
        if [ $? -eq 0 ]; then
            echo "虚拟机$vm3_name IP: $ip_vm3启动正常！"
        else
            echo "虚拟机$vm3_name启动失败！"
            res=1
        fi
        break
    fi
    sleep 10
done

# 虚拟机cpu与物理机cpu绑定成功
echo "1.执行命令： virsh vcpuinfo $vm3_name"
virsh vcpuinfo $vm3_name 
if [ $? -ne 0 ];then
    echo "查询vcpuinfo失败"
    res=1
else
    echo -e "\033[32m-----------------虚拟机CPU和I/O线程绑定查询成功，请人工判断打印信息中'CPU项'数据与'CPU Affinity'的y的位数是否一致，信息一致测试通过----------------\033[0m"
fi
echo -e "\n\n\n\n"
# 2.虚拟机I/O与物理机I/O绑定成功
echo "2.执行命令： virsh qemu-monitor-command $vm3_name --hmp \"info iothreads\""
virsh qemu-monitor-command $vm3_name --hmp "info iothreads" 
if [ $? -ne 0 ];then
    echo "查询qemu-monitor-command失败"
    res=1
else
    echo -e "\033[32m-----------------虚拟机I/O绑定查询成功，请人工判断打印信息中iothread信息与虚拟机XML配置文件是否一致，信息一致测试通过----------------\033[0m"
fi

